package be.bonamis.umpire.ui.client.gin;

import be.bonamis.umpire.ui.client.AppPlaceHistoryMapper;
import be.bonamis.umpire.ui.client.ClientFactory;
import be.bonamis.umpire.ui.client.mvp.BootView;
import be.bonamis.umpire.ui.client.mvp.DispoView;
import be.bonamis.umpire.ui.client.mvp.HistoryView;
import be.bonamis.umpire.ui.client.mvp.LoginView;
import be.bonamis.umpire.ui.client.mvp.QuizView;
import be.bonamis.umpire.ui.client.mvp.WhoView;
import be.bonamis.umpire.ui.client.mvp.impl.BootViewImpl;
import be.bonamis.umpire.ui.client.mvp.impl.DispoViewImpl;
import be.bonamis.umpire.ui.client.mvp.impl.HistoryViewImpl;
import be.bonamis.umpire.ui.client.mvp.impl.LoginViewImpl;
import be.bonamis.umpire.ui.client.mvp.impl.QuizViewImpl;
import be.bonamis.umpire.ui.client.mvp.impl.WhoViewImpl;
import be.bonamis.umpire.ui.client.places.LoginPlace;
import be.credoc.common.gwt.actions.client.AbstractActionServiceDelegate;
import be.credoc.common.gwt.actions.client.ActionServiceWrapper;
import be.credoc.common.gwt.actions.shared.service.ActionService;
import be.credoc.common.gwt.actions.shared.service.ActionServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

public class UmpireGinModule extends AbstractGinModule {

	@Override
	protected void configure() {
		bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
		bind(com.google.gwt.event.shared.EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
		bind(PlaceHistoryMapper.class).to(AppPlaceHistoryMapper.class).in(Singleton.class);

		bind(LoginView.class).to(LoginViewImpl.class).in(Singleton.class);		
		bind(BootView.class).to(BootViewImpl.class).in(Singleton.class);
		bind(DispoView.class).to(DispoViewImpl.class).in(Singleton.class);
		bind(WhoView.class).to(WhoViewImpl.class).in(Singleton.class);
		bind(QuizView.class).to(QuizViewImpl.class).in(Singleton.class);
		bind(HistoryView.class).to(HistoryViewImpl.class).in(Singleton.class);

		bind(ClientFactory.class).in(Singleton.class);
		bind(AbstractActionServiceDelegate.class).to(UmpireActionServiceDelegate.class).in(Singleton.class);
	}

	@Provides
	@Singleton
	public PlaceHistoryHandler getPlaceHistoryHandler(PlaceController placeController,
			PlaceHistoryMapper historyMapper, EventBus eventBus) {
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, new LoginPlace());
		return historyHandler;
	}

	@Provides
	@Singleton
	public PlaceController getPlaceController(EventBus eventBus) {
		return new PlaceController(eventBus);
	}

	@Provides
	@Singleton
	public ActionServiceWrapper getActionServiceWrapper(AbstractActionServiceDelegate abstractActionServiceDelegate) {
		ActionServiceAsync as = GWT.create(ActionService.class);
		((ServiceDefTarget) as).setServiceEntryPoint("service/action");
		return new ActionServiceWrapper(as, abstractActionServiceDelegate);
	}
}
